/*
 * Copyright (C), 2002-2016, 苏宁易购电子商务有限公司
 * FileName: CfClusterDBOperation.java
 * Author:   admin
 * Date:     2016年2月23日 上午11:54:13
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.suning.app.snpaasadmin.operation;

import com.suning.app.snpaasadmin.dao.CfClusterConfigMapper;
import com.suning.app.snpaasadmin.dao.CfClusterMapper;
import com.suning.app.snpaasadmin.dao.CfMysqlClusterConfigMapper;
import com.suning.app.snpaasadmin.dao.CfMysqlClusterMapper;
import com.suning.app.snpaasadmin.dao.CfMysqlServiceBrokersMapper;
import com.suning.app.snpaasadmin.dao.ClusterDeployTaskMapper;
import com.suning.app.snpaasadmin.dao.ServiceBrokerMysqlMapper;
import com.suning.app.snpaasadmin.pojo.CfCluster;
import com.suning.app.snpaasadmin.pojo.CfClusterConfig;
import com.suning.app.snpaasadmin.pojo.CfClusterConfigExample;
import com.suning.app.snpaasadmin.pojo.CfClusterExample;
import com.suning.app.snpaasadmin.pojo.CfMysqlCluster;
import com.suning.app.snpaasadmin.pojo.CfMysqlClusterConfig;
import com.suning.app.snpaasadmin.pojo.CfMysqlClusterConfigExample;
import com.suning.app.snpaasadmin.pojo.CfMysqlClusterExample;
import com.suning.app.snpaasadmin.pojo.CfMysqlServiceBrokers;
import com.suning.app.snpaasadmin.pojo.CfMysqlServiceBrokersExample;
import com.suning.app.snpaasadmin.pojo.ClusterDeployTask;
import com.suning.app.snpaasadmin.pojo.ClusterDeployTaskExample;
import com.suning.app.snpaasadmin.pojo.ServiceBrokerMysql;
import com.suning.app.snpaasadmin.pojo.ServiceBrokerMysqlExample;
import com.suning.app.snpaasadmin.util.Constants;
import com.suning.app.snpaasadmin.util.DbContextHolder;
import com.suning.app.snpaasadmin.util.StringUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author admin
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service
public class CfClusterDBOperation {

    @Autowired
    private CfClusterMapper cfClusterMapper;

    @Autowired
    private CfClusterConfigMapper cfClusterConfigMapper;

    @Autowired
    private CfMysqlClusterMapper mysqlClusterMapper;

    @Autowired
    private CfMysqlClusterConfigMapper mysqlClusterConfigMapper;

    @Autowired
    private CfMysqlServiceBrokersMapper MysqlServiceBrokersMapper;

    @Autowired
    private ServiceBrokerMysqlMapper serviceBrokerMysqlMapper;

    @Autowired
    private ClusterDeployTaskMapper clusterDeployTaskMapper;
    
    // 获取cf集群组件的cpu核数
    public Integer getCpuCore(String comName, String deployId) throws Exception {
        DbContextHolder.setDbType(Constants.DATASOURCE_ADMIN);
        CfClusterConfigExample configExample = new CfClusterConfigExample();
        configExample.createCriteria().andComponentEqualTo(comName).andDeployIdEqualTo(Integer.valueOf(deployId));
        List<CfClusterConfig> config = cfClusterConfigMapper.selectByExample(configExample);
        Integer value = 0;
        if (config != null && config.size() > 0) {
            return config.get(0).getCpuCore();
        }
        return value;
    }

    // 获取cf集群
    public List<CfCluster> getCfClusterAll(String depolyId) throws Exception {
        DbContextHolder.setDbType(Constants.DATASOURCE_ADMIN);
        List<CfCluster> clusters = new ArrayList<>();
        CfClusterExample example = new CfClusterExample();
        if (StringUtil.isNotEmpty(depolyId) && StringUtil.isNotNullString(depolyId)) {
            example.createCriteria().andIdEqualTo(Integer.valueOf(depolyId));
        } else {
            example.createCriteria();
        }
        clusters = cfClusterMapper.selectByExample(example);
        return clusters;
    }

    /**
     * 
     * 功能描述: <br>
     * 获取cf集群的名称
     *
     * @param id
     * @return
     * @throws Exception
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public String getDeployName(String id) throws Exception {
        DbContextHolder.setDbType(Constants.DATASOURCE_ADMIN);
        CfCluster cluster = cfClusterMapper.selectByPrimaryKey(Integer.valueOf(id));
        String name = cluster.getClusterName();
        return name;
    }

    /**
     * 
     * 功能描述: <br>
     * 保存cf集群
     *
     * @param cluster
     * @throws Exception
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public void insertClusterConfig(CfCluster cluster) throws Exception {
        DbContextHolder.setDbType(Constants.DATASOURCE_ADMIN);
        cfClusterMapper.insertSelective(cluster);
    }

    public void insertComponentConfig(CfClusterConfig clusterConfig) throws Exception {
        DbContextHolder.setDbType(Constants.DATASOURCE_ADMIN);
        cfClusterConfigMapper.insertSelective(clusterConfig);
    }

    public void updateClusterConfig(CfCluster cluster) throws Exception {
        DbContextHolder.setDbType(Constants.DATASOURCE_ADMIN);
        cfClusterMapper.updateByPrimaryKeySelective(cluster);
    }

    public void updateComponentConfig(CfClusterConfig clusterConfig) throws Exception {
        DbContextHolder.setDbType(Constants.DATASOURCE_ADMIN);
        cfClusterConfigMapper.updateByPrimaryKeySelective(clusterConfig);
    }

    public List<CfClusterConfig> selectComponentConfig(String deployId, String value) throws Exception {
        DbContextHolder.setDbType(Constants.DATASOURCE_ADMIN);
        CfClusterConfigExample example = new CfClusterConfigExample();
        if (StringUtil.isNotEmpty(value) && StringUtil.isNotNullString(value)) {
            example.createCriteria().andDeployIdEqualTo(Integer.valueOf(deployId)).andComponentEqualTo(value);
        } else {
            example.createCriteria().andDeployIdEqualTo(Integer.valueOf(deployId));
        }
        List<CfClusterConfig> configs = cfClusterConfigMapper.selectByExample(example);
        return configs;
    }

    public CfCluster selectCfCluster(String deployId) throws Exception {
        DbContextHolder.setDbType(Constants.DATASOURCE_ADMIN);
        CfCluster cluster = cfClusterMapper.selectByPrimaryKey(Integer.valueOf(deployId));
        return cluster;
    }

    public void updataMysqlConfig(CfMysqlCluster mysqlCluster) throws Exception {
        DbContextHolder.setDbType(Constants.DATASOURCE_ADMIN);
        mysqlClusterMapper.updateByPrimaryKeySelective(mysqlCluster);
    }

    public List<CfMysqlClusterConfig> selectMysqlConfig(String mysqlId) throws Exception {
        DbContextHolder.setDbType(Constants.DATASOURCE_ADMIN);
        CfMysqlClusterConfigExample example = new CfMysqlClusterConfigExample();
        example.createCriteria().andMysqlClusterIdEqualTo(Integer.valueOf(mysqlId));
        List<CfMysqlClusterConfig> mysqlClusterConfigs = mysqlClusterConfigMapper.selectByExample(example);
        return mysqlClusterConfigs;
    }

    public void updateMysqlComConfig(CfMysqlClusterConfig cfMysqlClusterConfig) throws Exception {
        DbContextHolder.setDbType(Constants.DATASOURCE_ADMIN);
        mysqlClusterConfigMapper.updateByPrimaryKeySelective(cfMysqlClusterConfig);
    }

    public void insertMysqlCluster(CfMysqlCluster cfMysqlCluster) throws Exception {
        DbContextHolder.setDbType(Constants.DATASOURCE_ADMIN);
        mysqlClusterMapper.insertSelective(cfMysqlCluster);
    }

    public void insertMysqlComConfig(CfMysqlClusterConfig config) throws Exception {
        DbContextHolder.setDbType(Constants.DATASOURCE_ADMIN);
        mysqlClusterConfigMapper.insertSelective(config);
    }

    public CfMysqlCluster selectMysqlCluster(String mysqlId) throws Exception {
        DbContextHolder.setDbType(Constants.DATASOURCE_ADMIN);
        CfMysqlCluster cluster = mysqlClusterMapper.selectByPrimaryKey(Integer.valueOf(mysqlId));
        return cluster;
    }

    public List<CfMysqlCluster> selectAllMysqlCluster() throws Exception {
        DbContextHolder.setDbType(Constants.DATASOURCE_ADMIN);
        CfMysqlClusterExample example = new CfMysqlClusterExample();
        example.createCriteria();
        List<CfMysqlCluster> mysqlClusters = mysqlClusterMapper.selectByExample(example);
        return mysqlClusters;
    }

    public void insertServiceBroker(CfMysqlServiceBrokers serviceBrokers) throws Exception {
        DbContextHolder.setDbType(Constants.DATASOURCE_SERVER);
        MysqlServiceBrokersMapper.insertSelective(serviceBrokers);
    }

    public void insertAdminServiceBroker(ServiceBrokerMysql broker) throws Exception {
        DbContextHolder.setDbType(Constants.DATASOURCE_ADMIN);
        serviceBrokerMysqlMapper.insertSelective(broker);
    }

    public String selectServiceBrokerUse(Integer mysqlId) throws Exception {
        DbContextHolder.setDbType(Constants.DATASOURCE_ADMIN);
        ServiceBrokerMysqlExample example = new ServiceBrokerMysqlExample();
        example.createCriteria().andMysqlIdEqualTo(mysqlId);
        List<ServiceBrokerMysql> brokerMysqls = serviceBrokerMysqlMapper.selectByExample(example);
        String guid = "";
        if (brokerMysqls != null && brokerMysqls.size() > 0) {
            guid = brokerMysqls.get(0).getServiceBrokeGuid();
        }
        return guid;
    }

    public boolean getBrokerUsage(String brokerId) throws Exception {
        boolean flag = true;
        DbContextHolder.setDbType(Constants.DATASOURCE_SERVER);
        CfMysqlServiceBrokersExample brokersExample = new CfMysqlServiceBrokersExample();
        brokersExample.createCriteria().andServiceBrokerGuidEqualTo(brokerId);
        List<CfMysqlServiceBrokers> cfMysqlServiceBrokers = MysqlServiceBrokersMapper.selectByExample(brokersExample);
        if (cfMysqlServiceBrokers != null) {
            if (Constants.SERVICE_BROKER_UNUSE.equals(cfMysqlServiceBrokers.get(0).getUseState())) {
                flag = false;
            }
        }
        return flag;
    }

    public List<CfMysqlClusterConfig> getMysqlCom(Integer mysqlId) throws Exception {
        DbContextHolder.setDbType(Constants.DATASOURCE_ADMIN);
        List<CfMysqlClusterConfig> configs = new ArrayList<>();
        CfMysqlClusterConfigExample example = new CfMysqlClusterConfigExample();
        example.createCriteria().andMysqlClusterIdEqualTo(mysqlId);
        configs = mysqlClusterConfigMapper.selectByExample(example);
        return configs;
    }

    public CfMysqlServiceBrokers getServiceBrokerDetail(String guid) throws Exception {
        DbContextHolder.setDbType(Constants.DATASOURCE_SERVER);
        CfMysqlServiceBrokers broker = new CfMysqlServiceBrokers();
        CfMysqlServiceBrokersExample example = new CfMysqlServiceBrokersExample();
        example.createCriteria().andServiceBrokerGuidEqualTo(guid);
        List<CfMysqlServiceBrokers> serviceBrokers = MysqlServiceBrokersMapper.selectByExample(example);
        if (serviceBrokers != null) {
            broker = serviceBrokers.get(0);
        }
        return broker;
    }

    public Integer getMysqlCpuCore(String comName, String deployId) throws Exception {
        DbContextHolder.setDbType(Constants.DATASOURCE_ADMIN);
        CfMysqlClusterConfigExample configExample = new CfMysqlClusterConfigExample();
        configExample.createCriteria().andMysqlClusterIdEqualTo(Integer.valueOf(deployId));
        List<CfMysqlClusterConfig> config = mysqlClusterConfigMapper.selectByExample(configExample);
        Integer value = 0;
        if (config != null && config.size() > 0) {
            return config.get(0).getCpuCore();
        }
        return value;
    }

    public List<Map<String, String>> getTasksInfo(String deployId, Integer type) throws Exception {
        DbContextHolder.setDbType(Constants.DATASOURCE_ADMIN);
        ClusterDeployTaskExample configExample = new ClusterDeployTaskExample();
        configExample.createCriteria().andDeployidEqualTo(deployId).andTypeEqualTo(type);
        configExample.setOrderByClause("create_time");
        List<ClusterDeployTask> config = clusterDeployTaskMapper.selectByExample(configExample);
        List<Map<String, String>> tasks = new ArrayList<>();
        for (ClusterDeployTask task : config) {
            Map<String, String> taskInfo = new HashMap<>();
            taskInfo.put("create_time", "" + task.getCreateTime().getTime());
            taskInfo.put("taskId", task.getTaskId());
            tasks.add(taskInfo);
        }
        return tasks;
    }

    public void deleteCfClusterData(String deployId) throws Exception {
        DbContextHolder.setDbType(Constants.DATASOURCE_ADMIN);
        cfClusterMapper.deleteByPrimaryKey(Integer.valueOf(deployId));
        CfClusterConfigExample example = new CfClusterConfigExample();
        example.createCriteria().andDeployIdEqualTo(Integer.valueOf(deployId));
        cfClusterConfigMapper.deleteByExample(example);
        ClusterDeployTaskExample taskExample = new ClusterDeployTaskExample();
        taskExample.createCriteria().andDeployidEqualTo(deployId).andTypeEqualTo(Constants.TYPE_CF_VERSION);
        clusterDeployTaskMapper.deleteByExample(taskExample);
    }

    public void insertTaskId(String taskId, String deployId, Integer type) throws Exception {
        DbContextHolder.setDbType(Constants.DATASOURCE_ADMIN);
        ClusterDeployTask task = new ClusterDeployTask();
        Integer id = 1;
        // 获取最后一个id
        ClusterDeployTaskExample example = new ClusterDeployTaskExample();
        example.createCriteria();
        List<ClusterDeployTask> deployTasks = clusterDeployTaskMapper.selectByExample(example);
        if (deployTasks != null && deployTasks.size() > 0) {
            id = deployTasks.get(deployTasks.size() - 1).getId() + 1;
        }
        task.setId(id);
        task.setCreateTime(new Date());
        task.setDeployid(deployId);
        task.setTaskId(taskId);
        task.setType(type);
        clusterDeployTaskMapper.insertSelective(task);
    }

    public void deleteMysqlData(String deployId) throws Exception {
        DbContextHolder.setDbType(Constants.DATASOURCE_ADMIN);
        mysqlClusterMapper.deleteByPrimaryKey(Integer.valueOf(deployId));
        CfMysqlClusterConfigExample example = new CfMysqlClusterConfigExample();
        example.createCriteria().andMysqlClusterIdEqualTo(Integer.valueOf(deployId));
        
        mysqlClusterConfigMapper.deleteByExample(example);

        ClusterDeployTaskExample taskExample = new ClusterDeployTaskExample();
        taskExample.createCriteria().andDeployidEqualTo(deployId).andTypeEqualTo(Constants.TYPE_MYSQL_VERSION);
        clusterDeployTaskMapper.deleteByExample(taskExample);
    }


}
